package com.ebelter.bpm.models.update;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;

import com.ebelter.bpm.R;
import com.ebelter.bpm.models.http.BpmNetUtils;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.UpdateVersionBean;
import com.ebelter.btcomlib.models.https.HttpsUrl;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.GsonUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.dialogs.TextTipDialog;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;


public class Updater {


    private static final String TAG = "Updater";



    private long lastCheckTime;
    public void checkVersion(final Activity activity, final boolean isShowDialogTip,final boolean isCompareOpenAppTime){

        if (System.currentTimeMillis()-lastCheckTime<2000){
            lastCheckTime=System.currentTimeMillis();
            return;
        }

        if (isCompareOpenAppTime){
            long openAppTime = SpUtil.readLong(Constants.IUser.OPEN_APP_TIME);
            if (System.currentTimeMillis()-openAppTime>5000){//检测app打开时间 如果app打开的时间与当前时间大于5秒就退出
                return;
            }
        }

        if (!NetUtils.available()||activity==null){
            return;
        }
        final int verCode= AppUtils.getVersionCode(activity);//获取当前版本号
        LogUtils.i(TAG,"appVerCode --"+verCode);
        BpmNetUtils.getInstance().getVersionJosn(activity, new HttpResponse<UpdateVersionBean>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, UpdateVersionBean updateVersionBean,String s) {
                if (isRequestSuccess){
                    LogUtils.i(TAG,"服务器上的json--"+updateVersionBean.toString());
                    if (NumUtils.string2Int(updateVersionBean.getVerCode())>verCode){
                        //说明有新版本
                        showUpdateDialog(activity,updateVersionBean);
                    }else {
                        if (isShowDialogTip){
                            ToastUtil.show(R.string.Latest);
                        }
                    }


                }
            }
        });




    }



    private TextTipDialog dialog;
    private void showUpdateDialog(final Activity activity, final UpdateVersionBean updateVersionBean) {
        if (activity==null||activity.isFinishing()||activity.isDestroyed()||updateVersionBean==null){
            return;
        }
        if (UpdateService.isDownLoading){
            return;
        }
        if (dialog!=null){
            dialog.cancel();
            dialog=null;
        }

        dialog=new TextTipDialog(activity, StringUtils.getResStr(R.string.New_version_is_available_touch_to_update));
        dialog.show();
        dialog.setNativebtVisibility(View.GONE);
        dialog.setListener(new TextTipDialog.ICancelOkBtListener() {
            @Override
            public void cancel(Dialog dialogView) {
                dialogView.cancel();
            }
            @Override
            public void ok(Dialog dialogView) {
                dialogView.cancel();
                if (!NetUtils.available()){
                    ToastUtil.show(R.string.Network_error);
                    return;
                }
                Intent updateIntent=new Intent(activity,UpdateService.class);
                updateIntent.putExtra(UpdateService.DownloadSer,updateVersionBean);
                activity.startService(updateIntent);

            }
        });
    }


}
